package com.yjhealth.libs.core.utils.location;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.yjhealth.libs.core.R;
import com.yjhealth.libs.core.log.CoreLogTag;
import com.yjhealth.libs.core.toast.ToastUtil;
import com.yjhealth.libs.core.utils.AppUtil;
import com.yjhealth.libs.core.utils.EffectUtil;
import com.yjhealth.libs.core.view.dialog.CoreDialog;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 跳转地图
 * <p>
 * 高德地图文档
 * https://lbs.amap.com/api/amap-mobile/guide/android/search
 * <p>
 * 百度地图文档
 * http://lbsyun.baidu.com/index.php?title=uri/api/android
 */
public class LocationUtil {

    /**
     * 有定位坐标
     *
     * @param activity
     * @param config
     */
    public static void showLocationDialog(final Activity activity, final LocationConfig config) {
        if (config == null) {
            return;
        }
        final CoreDialog dialog = new CoreDialog(LayoutInflater.from(activity).inflate(R.layout.yjhealth_core_dialog_location, null));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        LinearLayout llBaidu = dialog.findViewById(R.id.llBaidu);
        LinearLayout llGaode = dialog.findViewById(R.id.llGaode);

        EffectUtil.addClickEffect(llBaidu);
        llBaidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToBaiduMap(activity, config);
                dialog.dismiss();
            }
        });
        EffectUtil.addClickEffect(llGaode);
        llGaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToGaodeMap(activity, config);
                dialog.dismiss();
            }
        });

        dialog.show(activity);
    }

    /**
     * 没有定位坐标
     *
     * @param activity
     * @param config
     */
    public static void showLocationDialogSearch(final Activity activity, final LocationConfig config) {
        if (config == null) {
            return;
        }
        final CoreDialog dialog = new CoreDialog(LayoutInflater.from(activity).inflate(R.layout.yjhealth_core_dialog_location, null));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        LinearLayout llBaidu = dialog.findViewById(R.id.llBaidu);
        LinearLayout llGaode = dialog.findViewById(R.id.llGaode);

        EffectUtil.addClickEffect(llBaidu);
        llBaidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToBaiduMapSearch(activity, config);
                dialog.dismiss();
            }
        });
        EffectUtil.addClickEffect(llGaode);
        llGaode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToGaodeMapSearch(activity, config);
                dialog.dismiss();
            }
        });

        dialog.show(activity);
    }

    /**
     * 跳转百度地图 自定义打点
     * baidumap://map/marker?location=40.057406655722,116.2964407172&title=Marker&content=makeamarker&traffic=on&src=andr.baidu.openAPIdemo
     */
    public static void goToBaiduMap(Context context, LocationConfig config) {
        if (config == null) {
            Log.e(CoreLogTag.TAG, "LocationUtil;goToBaiduMap;config null");
            return;
        }
        if (!AppUtil.isInstalled("com.baidu.BaiduMap")) {
            ToastUtil.toast(R.string.yjhealth_core_no_baidu);
            return;
        }
        try {
            Intent intent = new Intent();
            StringBuffer stringBuffer = null;
            stringBuffer = new StringBuffer("baidumap://map/marker?title=")
                    .append(URLEncoder.encode(config.name, "utf-8"));
            if (config.latitude != null && config.longitude != null) {
                stringBuffer.append("&location=").append(config.latitude)
                        .append(",").append(config.longitude);
            }
            stringBuffer.append("&coord_type=").append((TextUtils.isEmpty(config.coordType) ? "bd09" : config.coordType));
            intent.setData(Uri.parse(stringBuffer.toString()));
            context.startActivity(intent); // 启动调用
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转百度地图 附近搜索
     * baidumap://map/place/search?query=美食&region=beijing&location=39.915168,116.403875&radius=1000&bounds=37.8608310000,112.5963090000,42.1942670000,118.9491260000&src=andr.baidu.openAPIdemo
     */
    public static void goToBaiduMapSearch(Context context, LocationConfig config) {
        if (config == null) {
            Log.e(CoreLogTag.TAG, "LocationUtil;goToBaiduMap;config null");
            return;
        }
        if (!AppUtil.isInstalled("com.baidu.BaiduMap")) {
            ToastUtil.toast(R.string.yjhealth_core_no_baidu);
            return;
        }
        try {
            Intent intent = new Intent();
            StringBuffer stringBuffer = null;
            stringBuffer = new StringBuffer("baidumap://map/place/search?query=")
                    .append(URLEncoder.encode(config.name, "utf-8"));
            stringBuffer.append("&src=andr.baidu.openAPIdemo");
            intent.setData(Uri.parse(stringBuffer.toString()));
            context.startActivity(intent); // 启动调用
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    /**
     * 跳转高德 地图标注
     * androidamap://viewMap?sourceApplication=appname&poiname=abc&lat=36.2&lon=116.1&dev=0
     */
    public static void goToGaodeMap(Context context, LocationConfig config) {
        if (config == null) {
            Log.e(CoreLogTag.TAG, "LocationUtil;goToGaodeMap;config null");
            return;
        }
        if (!AppUtil.isInstalled("com.autonavi.minimap")) {
            ToastUtil.toast(R.string.yjhealth_core_no_gaode);
            return;
        }
        try {
            StringBuffer stringBuffer = null;
            stringBuffer = new StringBuffer("androidamap://viewMap?sourceApplication=")
                    .append(URLEncoder.encode(AppUtil.getAppName(), "utf-8"));
            stringBuffer.append("&poiname=").append(URLEncoder.encode(config.name, "utf-8"));
            if (config.latitude != null && config.longitude != null) {
                stringBuffer.append("&lat=").append(config.latitude)
                        .append("&lon=").append(config.longitude);
            }
            stringBuffer.append("&dev=").append(0);
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(stringBuffer.toString()));
            intent.setPackage("com.autonavi.minimap");
            context.startActivity(intent);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转高德 搜索地点
     * androidamap://poi?sourceApplication=softname&keywords=银行|加油站|电影院&lat1=36.1&amp;lon1=116.1&lat2=36.2&lon2=116.2&dev=0
     * androidamap://poi?sourceApplication=softname&keywords=银行|加油站|电影院&dev=0
     */
    public static void goToGaodeMapSearch(Context context, LocationConfig config) {
        if (config == null) {
            Log.e(CoreLogTag.TAG, "LocationUtil;goToGaodeMap;config null");
            return;
        }
        if (!AppUtil.isInstalled("com.autonavi.minimap")) {
            ToastUtil.toast(R.string.yjhealth_core_no_gaode);
            return;
        }
        try {
            StringBuffer stringBuffer = null;
            stringBuffer = new StringBuffer("androidamap://poi?sourceApplication=")
                    .append(URLEncoder.encode(AppUtil.getAppName(), "utf-8"));
            stringBuffer.append("&keywords=").append(URLEncoder.encode(config.name, "utf-8"));
            stringBuffer.append("&dev=").append(0);
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(stringBuffer.toString()));
            intent.setPackage("com.autonavi.minimap");
            context.startActivity(intent);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


}
